package com.zjl.core.redis;


import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.session.Session;

import java.util.*;

/**
 * @author : zhangjl
 * @Description :
 * @Created : 2018-05-31 10:19
 **/
@Slf4j
public class RedisShiroCache {

    private static final String hashKey = "shiroSessionKey";

    /**
     * 删除
     *
     * @param id
     */
    public static void hdelete(String id) {
        RedisUtil.hashDelete(id, hashKey);
    }

    /**
     * 添加
     *
     * @param id
     * @param object
     */
    public static void hAdd(String id, Object object) {
        RedisUtil.hashPut(id, hashKey, object);
    }

    /**
     * 获取
     *
     * @param id
     * @return
     */
    public static Session hGet(String id) {
        Object obj = RedisUtil.hashGet(id, hashKey);
        String result = JSON.toJSONString(obj);
        log.info("result:{}", result);
        Session session = JSON.parseObject(result, Session.class);
        return (Session)obj;
    }

    public static void main(String[] args) {


    }

    /**
     * 返回全部的缓存
     *
     * @return
     */
    public static List<Session> hGetList() {
        List<Session> list = new ArrayList<>(1000);
        List<Object> values = RedisUtil.hashValues(hashKey);
        for (Object obj : values) {
            list.add((Session) obj);
        }
        return list;
    }
}
